define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");

	var Model = function() {
		this.callParent();
	};

	Model.prototype.OK_ButtonClick = function(event) {
		console.log("OK_ButtonClick()");
		var data = {};
		data.username = window.app.username;
		data.password = window.app.password;
		data.CtmID = window.app.UserApi_ret.CtmID;
		data.CtmName = this.comp("CtmName_input").val();
		data.Contact = this.comp("Contact_input").val();
		data.Phone = this.comp("Phone_input").val();
		data.Telephone = "";
		data.Address = this.comp("Address_textarea").val();
		data.Province = "";
		data.City = "";
		data.County = "";
		data.CtmType= window.app.CtmType;

		console.log(data);
		if (data.Phone.length < 11) {
			justep.Util.hint("手机号码必须正确填写");
			return;
		}
		if (data.CtmName.length < 1) {
			justep.Util.hint("公司名称不能为空");
			return;
		}
		if (data.Contact.length < 1) {
			justep.Util.hint("负责人姓名不能为空");
			return;
		}

		var api_url = window.app.base_url + window.app.CustomerOneCreate;
		var self = this;
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.log(res);
				if (res.length === 0) {
					this.error();
					return;
				}
				if (res.errno === 0) {
					justep.Util.hint("新增分销商成功.");
					self.getParent().comp("DistributorManagement_Window").load("Distributor/Subagent.w");
					self.getParent().comp("Main_Contents").to("DistributorManagement_Content");
					return;
				}
				if (res.errno === 1 || res.errno === 2 || res.errno === 3) {
					justep.Util.hint(res.msg);
					return;
				}

			},
			error : function(err) {
				justep.Util.hint("服务器出错，新增分销商失败.");
				return;
			}
		});

	};

	Model.prototype.Cancel_ButtonClick = function(event) {
		this.getParent().comp("Main_Contents").to("DistributorManagement_Content");
	};

	Model.prototype.modelParamsReceive = function(event) {
		this.comp("CtmName_input").val("");
		this.comp("Contact_input").val("");
		this.comp("Phone_input").val("");
		this.comp("Address_textarea").val("");
	};

	return Model;
});